package com.t26.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.t26.web.entity.Backend_user;
import com.t26.web.service.Backend_userService;
import com.t26.web.vo.Json;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.t26.web.vo.ResultData;
import com.t26.web.vo.ResultDataGenerator;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;


/**
 *
 * @author Whenlean
 * @since 2021-06-12
 */

@RestController
@RequestMapping("/backend_user")
public class Backend_userController extends BaseController{

    @Autowired
    private Backend_userService backend_userService;

    @PostMapping("/login")
    public Json userLogin(@RequestBody String body){

        JSONObject jsonObject = JSON.parseObject(body);
        String userCode = jsonObject.getString("userCode");
        String userPassword = jsonObject.getString("userPassword");
        String oper = "select user";
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userCode",userCode);
        wrapper.eq("userPassword",userPassword);
        try {
            Backend_user user = backend_userService.getOne(wrapper);
            if(user==null) throw new AuthenticationException();
            logger.info("backend_user:login" + user.getUserName());
            return Json.succ(oper)
                    .data("token", UUID.randomUUID().toString())
                    .data("uid",user.getId())
                    .data("nick",user.getUserCode())
                    .data("roles",user.getUserType())
                    .data("perms","");
        } catch ( UnknownAccountException uae ) {
            logger.info("用户帐号不正确");
            return Json.fail(oper,"用户帐号或密码不正确");

        } catch ( IncorrectCredentialsException ice ) {
            logger.info("用户密码不正确");
            return Json.fail(oper,"用户帐号或密码不正确");

        } catch ( LockedAccountException lae ) {
            logger.info("用户帐号被锁定");
            return Json.fail(oper,"用户帐号被锁定不可用");

        } catch ( AuthenticationException ae ) {
            logger.info("登录出错");
            return Json.fail(oper,"登录失败："+ae.getMessage());
        }
    }

    @GetMapping("/loginout")
    public Json loginout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return Json.succ("退出成功");
    }

    @GetMapping("getUserByCode")
    public ResultData loginout(@RequestParam("userCode") String userCode){
        QueryWrapper<Backend_user> wrapper = new QueryWrapper<>();
        wrapper.eq("userCode",userCode);
        Backend_user user = backend_userService.getOne(wrapper);

        return ResultDataGenerator.genSuccessResult(user,"查询成功");
    }


}

